import 'package:casdon/build_gen/assets.gen.dart';
import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/common/widget/badge_icon.dart';
import 'package:casdon/modules/lifeCircle/data/model/life_topic_module.dart';
import 'package:casdon/modules/lifeCircle/ui/widgets/author_header_widget.dart';
import 'package:casdon/modules/lifeCircle/ui/widgets/nine_gird_widget.dart';
import 'package:casdon/modules/lifeCircle/utils/count_format_utils.dart';
import 'package:casdon/auto_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 生活圈话题列表项
class TopicItemWidget extends StatelessWidget {
  final LifeTopicModule topic;
  final bool? showFollowBtn;
  final VoidCallback? onFollowTap;
  final VoidCallback? onAvaterTap;
  final VoidCallback? onCommentTap;
  final VoidCallback? onContentTap;
  final void Function(LifeTopicModule)? onZanTap;
  final void Function(int, String)? onImageTap;
  final void Function(int, String)? onVideoTap;

  const TopicItemWidget({
    super.key,
    required this.topic,
    required this.showFollowBtn,
    this.onFollowTap,
    this.onAvaterTap,
    this.onCommentTap,
    this.onZanTap,
    this.onImageTap,
    this.onVideoTap,
    this.onContentTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(vertical: 16.w),
      margin: EdgeInsets.only(bottom: 8.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: AuthorHeaderWidget(
              avatarUrl: topic.avatar,
              nickname: topic.userNickname.trim().isEmpty
                  ? "匿名用户"
                  : topic.userNickname,
              isOfficial: false,
              createTime: topic.createTime,
              isFollowed: topic.isFollow,
              showFollowBtn: showFollowBtn,
              onFollowTap: topic.isMyTopic == false ? onFollowTap : null,
              onAvaterTap: onAvaterTap ?? () => _handleAvaterTap(context),
            ),
          ),

          SizedBox(height: 8.h),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child: NineGridWidget(
              title: topic.title,
              content: topic.content.strippingHTML(),
              images: topic.imageList(),
              videoUrls: topic.videoList(),
              onImageTap: (index, url) =>
                  onImageTap ??
                  _handleImageTap(context, topic.mediaSrcList, index),
              onVideoTap: (index, url) =>
                  onVideoTap ?? _handleVideoTap(context, url),
              onContentTap: () {
                if (onContentTap != null) {
                  onContentTap!();
                } else {
                  _handleCommentTap(context);
                }
              },
            ),
          ),

          SizedBox(height: 12.h),

          // 操作行（评论/点赞） - 保持与标题对齐（左右 16）
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                BadgeIcon(
                  imagePath: Assets.images.icComment.path,
                  size: 18.r,
                  onPressed: () {
                    if (onCommentTap != null) {
                      onCommentTap!();
                    } else {
                      _handleCommentTap(context);
                    }
                  },
                ),
                SizedBox(width: 4.w),
                Text(CountFormatUtils.formatCount(topic.comments)),
                SizedBox(width: 16.w),
                BadgeIcon(
                  imagePath: topic.isLike
                      ? Assets.images.icZaned.path
                      : Assets.images.icZan.path,
                  size: 18.r,
                  onPressed: () {
                    if (onZanTap != null) {
                      onZanTap!(topic);
                    } else {
                      _handleZanTap(context);
                    }
                  },
                ),
                SizedBox(width: 4.w),
                Text(CountFormatUtils.formatCount(topic.likes)),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void _handleCommentTap(BuildContext context) {
    LifeTopicDetailRoute(topicId: topic.id).push(context);
  }

  void _handleZanTap(BuildContext context) {}

  void _handleVideoTap(BuildContext context, String url) {
    VideoPlayerRoute(title: '', url: url).push(context);
  }

  void _handleContentTap(BuildContext context) {
    onContentTap?.call();
  }

  void _handleImageTap(
    BuildContext context,
    List<String> imageUlrList,
    int initIndex,
  ) {
    ImageBrowserRoute(
      imageUrls: imageUlrList,
      initialIndex: initIndex,
    ).push(context);
  }

  void _handleFollowTap(BuildContext context) {
    // 默认处理，如果外部没有提供回调，则不做任何操作
    // 外部应该通过 onFollowTap 提供处理逻辑
  }

  void _handleAvaterTap(BuildContext context) {}
}
